package com.example.job_listener;

import org.springframework.batch.core.*;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.listener.JobListenerFactoryBean;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

import javax.annotation.Resource;
import java.util.Map;

/***
 *
 */
@SpringBootApplication
@EnableBatchProcessing //启用springbatch框架 需要的注解
public class SpringBatchApplication5 {

    // job调度器
    @Autowired
    private JobLauncher jobLauncher;

    // job构建工厂
    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    // step步骤构建工厂
    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    @Resource(name = "myJobStateListener")
    private JobStateListener jobStateListener;


//    @Resource(name = "myAnnJobStateListener")
//    private JobAnnStateListener jobAnnStateListener;


    // 任务step执行逻辑由 tasklet完成
//    @StepScope //等到调用的时候再加载这个bean对象
    @Bean
    public Tasklet tasklet1() {
        return (stepContribution, chunkContext) -> {

            // 获取job/step 的 ExecutionContext
            ExecutionContext executionContext = chunkContext.getStepContext().getStepExecution().getExecutionContext();
            ExecutionContext executionContext1 = chunkContext.getStepContext().getStepExecution().getJobExecution().getExecutionContext();



            JobExecution jobExecution = stepContribution.getStepExecution().getJobExecution();
            BatchStatus status = jobExecution.getStatus();
            System.err.println("执行中的状态 status = " + status);

            // 返回执行任务完成
            return RepeatStatus.FINISHED;
        };
    }

    // 作业步骤 不带不读写操作
    @Bean
    public Step step1() {
        // 如果是通过延时加载去获取参数 那么这里的入参就置为null
        return stepBuilderFactory.get("step1").tasklet(tasklet1()).build();
    }



    @Bean
    public Job job() {
        return jobBuilderFactory
                .get("job-listener-ann-1")
//                .incrementer(new RunIdIncrementer())    // 使用增量器(run.id自增)
//                .incrementer(jobParametersIncrementer)    // 使用日期增量器
                .start(step1())
//                .listener(jobStateListener)          //添加监听器
//                .listener(jobAnnStateListener)          //添加监听器
                .listener(JobListenerFactoryBean.getListener(new JobAnnStateListener()))     //通过JobListenerFactoryBean 获取，添加监听器

                .build();
    }

    public static void main(String[] args) {
        SpringApplication.run(SpringBatchApplication5.class, args);
    }

}
